package com.oauth.config;

import com.oauth.TickGranter.CasTickAuthenticationProvider;
import com.oauth.serviceImpl.MyUserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;


/**
 * @author ysy
 * 1. 注解说明
 * A. prePostEnabled = true：会开启 @PreAuthorize 和 @PostAuthorize 两个注解。
 * @PreAuthorize注解会在方法执行前进行验证，支持Spring EL表达式；
 * @PostAuthorize 注解会在方法执行后进行验证，不经常使用，
 * 适用于验证带有返回值的权限。Spring EL提供了returnObject，用于能够在表达式语言中获取返回的对象信息；
 * B. securedEnabled = true：会开启@Secured 注解，
 * 用来定义业务方法的安全配置，在调用的接口或方法上使用该注解。
 * 在需要安全控制(一般使用角色或者权限进行控制)的方法上指定@Secured，
 * 达到只有具备那些角色/权限的用户才可以访问该方法。指定角色时必须以ROLE_开头，
 * 不可省略；不支持Spring EL表达式；如果想要使用@Secured注解指定"AND"条件，
 * 即调用deleteAll方法需同时拥有ADMIN和DBA角色的用户时，@Secured便不能实现，
 * 只能使用@PreAuthorize/@PostAuthorize注解。
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled =true )
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {



    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(CasTickAuthenticationProvider());
        auth.authenticationProvider(daoAuthProvider());
       // super.configure(auth);
    }
//    @Bean
//    public BCryptPasswordEncoder encoder() {
//        return new BCryptPasswordEncoder();
//    }

//    @Bean
//    UserDetailsService daoUserDetailsService() {
//        return new MyUserDetailServiceImpl();
//    }
    @Bean
    public DaoAuthenticationProvider daoAuthProvider() {
        final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService());
        authProvider.setPasswordEncoder(new BCryptPasswordEncoder());
        return authProvider;
    }
    /**
     * 从父类加载认证管理器
     */

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Bean
    public AuthenticationProvider CasTickAuthenticationProvider(){
        CasTickAuthenticationProvider provider = new CasTickAuthenticationProvider();
        return provider;
    }

    /**
     * 走内存，我们可以自己定义走数据库把注入即可,
     * @return
     */
    @Override
    @Bean
    public UserDetailsService userDetailsService(){
//        InMemoryUserDetailsManager userDetails =  new InMemoryUserDetailsManager();
//        userDetails.createUser((User.withUsername("admin")
//                .password(passwordEncoder().encode("admin"))
//                .authorities("mobile","salary").build()));
        // Impl需要加入@Service("userDetailsService")
        return new MyUserDetailServiceImpl();
    }


    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin();

    }
}
